package com.dfiles.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dfiles.Config;
import com.dfiles.R;
import com.dfiles.bean.DFile;
import com.dfiles.provider.FileIconHelper;
import com.dfiles.utils.DateUtils;
import com.dfiles.utils.FileUtils;
import com.dfiles.utils.UIHelper;

/**
 * gridview单个项
 */
public class FileGridItem extends FrameLayout {

	private View mainView;
	private Context context;
	private ImageView thumbnail;
	private ImageView thumbnailInner;
	private TextView textName;
	private TextView textSize;
	private TextView textLastModify;
	private DFile ebtFile;
	private int flagViewType = Config.UI.FLAG_VIEW_GRID;
	private FileIconHelper iconHelper;

	public FileGridItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public FileGridItem(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public FileGridItem(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public FileGridItem(Context context, int flagViewType, FileIconHelper iconHelper) {
		this(context);
		this.context = context;
		this.flagViewType = flagViewType;
		this.iconHelper = iconHelper;
		initView();
	}

	private void initView() {
		if (flagViewType == Config.UI.FLAG_VIEW_DETAIL) {// 详细列表
			mainView = (View) inflate(context, R.layout.file_detail_item, this);
			thumbnail = (ImageView) mainView.findViewById(R.id.file_detail_img_thumbnails);
			thumbnailInner = (ImageView) mainView.findViewById(R.id.file_detail_img_thumbnails_inner);
			textName = (TextView) mainView.findViewById(R.id.file_details_text_title);
			textSize = (TextView) mainView.findViewById(R.id.file_details_text_size);
			textLastModify = (TextView) mainView.findViewById(R.id.file_details_text_lastmodify);
		} else if (flagViewType == Config.UI.FLAG_VIEW_GRID) {// 大图显示
			mainView = inflate(context, R.layout.file_grid_item, this);
			thumbnail = (ImageView) mainView.findViewById(R.id.grid_file_img_thumbnails);
			thumbnailInner = (ImageView) mainView.findViewById(R.id.grid_file_img_thumbnails_inner);
			textName = (TextView) mainView.findViewById(R.id.grid_file_textview_name);
			textSize = (TextView) mainView.findViewById(R.id.grid_file_textview_filesize);
			textLastModify = (TextView) mainView.findViewById(R.id.grid_file_textview_updatedate);
		}
	}

	/**
	 * 填充数据
	 * 
	 * @param repository
	 */
	public void setData(DFile file, boolean selected) {
		if (file == null) {
			return;
		}
		this.ebtFile = file;
		textName.setText(file.name);
		String fileSize = FileUtils.formatFileSize(ebtFile.size);
		String lastModify = DateUtils.getDateStrByLong(ebtFile.lastModify);
		if (null == fileSize || "".equals(fileSize)) {
			textSize.setVisibility(GONE);
		} else {
			textSize.setText(fileSize);
		}
		if (null == lastModify || "".equals(lastModify)) {
			textLastModify.setVisibility(GONE);
		} else {
			textLastModify.setText(lastModify);
		}

		textName.setTextColor(Color.WHITE);// 名字颜色为白色
		textLastModify.setTextColor(Color.WHITE);
		textSize.setTextColor(Color.WHITE);
		if (flagViewType == Config.UI.FLAG_VIEW_DETAIL) {
			textLastModify.setTextColor(context.getResources().getColor(R.color.hint_color));
			textSize.setTextColor(context.getResources().getColor(R.color.hint_color));
			textName.setTextColor(Color.BLACK);
		}
		Drawable draw = context.getResources().getDrawable(UIHelper.toggleSelectedRes(context, selected));
		setForeground(draw);
		iconHelper.setIcon(file, flagViewType, textName, thumbnailInner, thumbnail);
	}

}
